package com.itheima.health.controller;

import com.itheima.health.dto.CheckGroupDto;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.vo.CheckGroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "检查组管理开发")
//@PreAuthorize("hasRole('ADMIN')") //角色认可 默认加ROLE_前缀
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;

    @GetMapping("checkgroup/findAll")
    @ApiOperation(value = "查询功能", notes = "查询所有的检查组列表信息")
    // 权限认可 一般写在方法上
//    @PreAuthorize("hasAnyAuthority('QUERY')")
    @PreAuthorize("hasRole('ADMIN')")
    public Result findAll() {
        return new Result(checkGroupService.list());
    }

    @PostMapping("checkgroup/findPage")
    @ApiOperation(value = "分页查询", notes = "分页查询所有的检查组列表信息")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        return new Result(checkGroupService.findPage(queryPageBean));
    }

    @GetMapping("checkgroup/findCheckIteminfosByGroupId/{id}")
    @ApiOperation(value = "查询功能", notes = "根据检查组id查询对应检查项明细")
    public Result findCheckIteminfosByGroupId(@PathVariable("id") int id) {
        CheckGroupVO checkGroupVO = checkGroupService.findCheckIteminfosByGroupId(id);
        return new Result(checkGroupVO);
    }

    @PostMapping("checkgroup/saveOrUpdateCheckGroup")
    public Result saveOrUpdatCheckGroup(@RequestBody CheckGroupDto checkGroupDto) {
        return new Result(checkGroupService.saveOrUpdatCheckGroup(checkGroupDto));
    }

    @DeleteMapping("checkgroup/deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(@RequestParam("id") Integer id) {
        try {
            CheckGroup checkGroup = new CheckGroup();
            checkGroup.setId(id);
            checkGroup.setIs_delete(1);
            checkGroupService.updateById(checkGroup);
            return new Result(true, "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "fail");
        }

    }
}
